<div class="tmd-doc">
<p></p>
<h1 class="tmd-header-1">
const 4
</h1>
<p></p>
<div id="question" class="tmd-base">
<p></p>
<div class="tmd-usual">
What is the behavior of the following program?
</div>
<p></p>
<pre class="tmd-code line-numbers">
<code class="language-go">package main

import "unsafe"

const S = "go" // S[1]-S[0] == 8

func main() {
	var x *[8][8]byte
	println(unsafe.Sizeof((*x)[S[1]-S[0]][S[1]-S[0]]))
}
</code></pre>
</div>
<p></p>
<div class="tmd-usual">
Choices:
</div>
<p></p>
<input type="radio" id="choiceA" name="choice" class="choice">
<input type="radio" id="choiceB" name="choice" class="choice">
<input type="radio" id="choiceC" name="choice" class="choice">

<div id="choices">

<ul style="list-style-type:none;">
<li><label for="choiceA">It prints 1.</label></li>
<li><label for="choiceB">It panics at run time.</label></li>
<li><label for="choiceC">It fails to compile.</label></li>
</ul>

</div><p></p>
<div id="answer" class="tmd-base">
<div class="tmd-usual">
Answer: it princs 1.
</div>
<p></p>
<div class="tmd-usual">
Run it on <a href="https://go.dev/play/p/ElskwAlPNxw">Go play</a>.
</div>
<p></p>
<div class="tmd-usual">
Key points:
</div>
<ul class="tmd-list">
<li class="tmd-list-item">
<div class="tmd-usual">
For a constant string <code class="tmd-code-span">S</code>, the expression <code class="tmd-code-span">S[i]</code> is always treated as a non-constant (please read the <a href="https://go101.org/article/string.html">stirngs in Go</a> article or <a href="https://go101.org/details-and-tips/101.html">Go Details and Tips</a> book). So the program compiles.
</div>
</li>
<li class="tmd-list-item">
<div class="tmd-usual">
<code class="tmd-code-span">unsafe.Sizeof</code> calls are evaluated at compile time. In the evaluations, only the type information of the arguments are used. The expressions <code class="tmd-code-span">*x</code> and <code class="tmd-code-span">S[1]-S[0]</code> etc are never evaluated at run time. So no "index out of range" panics will occur.
</div>
</li>
</ul>
<p></p>
</div>
<p></p>
</div>
